package com.qf.utils;

import com.qf.entity.Annunciate;
import com.qf.entity.User;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.List;

public class GzwUtil {
        //1、创建一个excle文件
         static HSSFWorkbook hssfWorkbook;
         //2、创建文件中的sheet表，参数是sheet名称
         static HSSFSheet sheet;
         //创建名，创建表的第一行
        public static void createFirstRow(String sheetName){
            hssfWorkbook=new HSSFWorkbook();
            sheet=hssfWorkbook.createSheet(sheetName);
            //创建第一行的字段名
            HSSFRow row = sheet.createRow(0);
            String[] heads={"名称","发布人","发布时间"};
            for (int i = 0; i < heads.length; i++) {
                //创建列
                HSSFCell cell = row.createCell(i);
                //每一列的值
                cell.setCellValue(heads[i]);
            }
        }

        //3、循环创建其他行的值 导出数据
    public static void createSheetValue(List<Annunciate> annunciates){

        //向这个表中的其他行赋值
        for (int i = 0; i < annunciates.size(); i++) {
            //创建第二行及以后
            HSSFRow row = sheet.createRow(i + 1);
            //当前行的值
            Annunciate annunciate  = annunciates.get(i);
            //向第二行及以后的每一列中赋值

            //第二行第一列
            HSSFCell cell1 = row.createCell(0);
            cell1.setCellValue(annunciate.getAnnName());
            //第二行第二列
            HSSFCell cell2 = row.createCell(1);
            cell2.setCellValue(annunciate.getRealName());
            //第二行第三列
            HSSFCell cell3 = row.createCell(2);
            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
            String format = sd.format(annunciate.getPublishTime());
            cell3.setCellValue(format);

        }
    }

    //excle输出 字节输出
    public static void export(OutputStream outputStream) throws IOException {

        //以网格的格式输出
        sheet.setGridsPrinted(true);
        //创建一个文件
        //以字节流响应给用户
        hssfWorkbook.write(outputStream);
        outputStream.flush();
    }


}
